package com.sdry.web.controller.gemini;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.PartPosition;
import com.sdry.entity.PartPositionExample;
import com.sdry.entity.PartPositionExample.Criteria;
import com.sdry.entity.PartStore;
import com.sdry.entity.PartStoreExample;
import com.sdry.entity.permission.UpmsStaffRole;
import com.sdry.service.gemini.PartPositionService;
import com.sdry.service.gemini.PartStoreService;
import com.sdry.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Gemini on 2018/7/31
 *
 * 库位信息控制类
 */

@Controller
@RequestMapping("/base/location")
public class BaseLocationInfoController extends BaseController {

    @Resource
    private PartPositionService positionService;

    /**
     * 去库位信息页面
     * @return
     */
    @RequestMapping(value = "/baseInfoLocation", method = RequestMethod.GET)
    public String baseInfoLocation() {

        return "/gemini/info_location";
    }

    /**
     * 添加库位信息
     * @param response 响应
     * @param position 库位实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertBaseLocationInfo")
    public void insertLocation(HttpServletRequest request,HttpServletResponse response, PartPosition position) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        position.setAreaptime(df.format(new Date()));//给录入时间赋值
        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

        String name = null;
        if(staffSession != null){
            name = staffSession.getUpmsStaff().getSname();
        }
        position.setAreapeople(name);
        //添加
        int affact = positionService.insert(position);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询库位信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param position 库位实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseLocationInfo")
    public void selectLocationList(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "limit",required = false)int limit,
                                     @RequestParam(value = "offset",required = false)int offset,
                                     PartPosition position) throws Exception {

        PageHelper.startPage(offset, limit);

        PartPositionExample example = new PartPositionExample();
        Criteria criteria = example.createCriteria();
        if (position.getAreanumbering() != null && !("").equals(position.getAreanumbering())){
            criteria.andAreanumberingLike("%"+position.getAreanumbering()+"%");
        }

        System.out.print("@@@@@@@@@@@@@@@@@"+position.getAreastorehouse());
        if (position.getAreastorehouse() != null && !("").equals(position.getAreastorehouse())){
            criteria.andAreastorehouseLike("%"+position.getAreastorehouse()+"%");
        }

        List<PartPosition> positions = positionService.selectAll(example);

        PageInfo pageInfo = new PageInfo(positions);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", pageInfo.getList());
        ResponseUtil.write(response, jsonObject);
    }





    /**
     * 根据id查询库位信息
     * @param response 响应
     * @param id 库位id
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseLocationInfoById")
    public void selectLocationById(HttpServletResponse response, Long id) throws Exception {

        PartPosition position = positionService.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(position);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改库位信息
     * @param response 响应
     * @param position 库位信息
     * @throws Exception 异常
     */
    @RequestMapping("/editBaseLocationInfo")
    public void editLocation(HttpServletRequest request, HttpServletResponse response,
                              PartPosition position) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        position.setAreaptime(df.format(new Date()));//给录入时间赋值
        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

        String name = null;
        if(staffSession != null){
            name = staffSession.getUpmsStaff().getSname();
        }
        position.setAreapeople(name);
        //修改
        int affct = positionService.updateByPrimaryKey(position);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除库位信息
     * @param response 响应
     * @param ids 库位id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteBaseLocationInfo")
    public void deleteLocation( HttpServletResponse response, String ids) throws Exception {
        int affact = positionService.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
}
